<template>
  <el-container class="home-container">
      <!-- 头部区 -->
    <el-header>
        <div>
            <img src="../assets/wonderland.jpg" alt="">
            <span>后台电商管理系统</span>
        </div>
        <el-button type="info" @click="logout">退出</el-button>
    </el-header>
    <!-- 页面主体区 -->
    <el-container>
        <!-- 侧边栏 -->
        <el-aside :width="isCollapse?'64px':'200px'">
            <!-- 控制折叠与展开 -->
            <div class="toggle-button" @click="toggleCollpase">|||</div>
            <!-- 菜单区 -->
            <el-menu background-color="#333744" text-color="#fff"
            active-text-color="#ffd04b" :unique-opened="true" 
            :collapse="isCollapse" :collapse-transition="false"
            :router="true" :default-active="$router.path">
                <!-- 一级菜单 -->
                <el-submenu :index="item.id.toString()" v-for="item in menuList" :key="item.id">
                    <!-- 一级菜单模板区 -->
                    <template slot="title">
                        <i :class="iconsObj[item.id]"></i>
                        <span>{{item.authName}}</span>
                    </template>
                    <!-- 二级菜单（menu开启路由后，点击二级导航会跳转到index绑定的路径中） -->
                    <el-menu-item :index="'/'+subItem.path" v-for="subItem in item.children" :key="subItem.id">
                        <!-- 二级菜单模板区 -->
                        <template slot="title">
                            <i class="el-icon-menu"></i>
                            <span>{{subItem.authName}}</span>
                        </template>
                    </el-menu-item>
                </el-submenu>  
            </el-menu>
        </el-aside>
        <!-- 右侧主题区 -->
        <el-main>
            <router-view></router-view>
        </el-main>
    </el-container>
  </el-container>
</template>

<script>
export default {
    data(){
        return{
            menuList:[],
            iconsObj:{
                '125':'iconfont icon-yonghuguanli',
                '103':'iconfont icon-fuwuquanxianguanli',
                '101':'iconfont icon-shangpinguanli',
                '102':'iconfont icon-dingdanguanli',
                '145':'iconfont icon-shujuguanli'
            },
            isCollapse:false
        }
    },
    created(){
        this.getMenuList();
    },
    methods:{
        logout(){
            window.sessionStorage.removeItem('token');
            this.$router.push('/login');
        },
        // 之前是post发起请求，现在是get请求服务器中的menus
        async getMenuList(){
            const {data:res} = await this.$http.get('menus');
            if(res.meta.status!==200) return this.$message.error(res.meta.msg);
            this.menuList = res.data;
        },
        // 控制是否折叠collapse
        toggleCollpase(){
            this.isCollapse=!this.isCollapse;
        }
    }
}
</script>

<style lang="less" scoped>
    .home-container{
        height: 100%;
    }
    .el-header{
        display: flex;
        justify-content: space-between;
        align-items: center;
        color: #fff;
        font-size: 20px;
        background-color: #373d41;
        padding: 0;
       div{
            display: flex;
            align-items: center;
           img{
               width: 61px;
               height: 56px;
               border-radius: 50%;
               margin-right: 15px;
           }
          
       }
    }
    .el-aside{
        background-color: #333744;
    }
    .el-main{
        background-color: #EAEDF1;
    }
    .iconfont{
        margin-right: 10px;
    }
    .el-menu{
        border-right: 0;
    }
    .toggle-button{
        color:#fff;
        font-size: 10px;
        line-height: 24px;
        text-align: center;
        background-color: #4a5064;
        letter-spacing: 0.2em;
        cursor: pointer;
    }
</style>